<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

if (!function_exists('load_ci')) {

    function load_ci() {
        return get_instance();
    }

}

if (!function_exists('get_comments')) {

    function get_comments($id) {
        $ci = load_ci();		
		return $ci->db->get_where('mcs_comments',array('announcement_id'=>$id))->result_array();
    }

}

if (!function_exists('get_label')) {

    function get_label($table,$field) {
        $ci = load_ci();		
		$label = $ci->db->get_where('su-tb_sys_validation',array('strTable'=>$table,'strField'=>$field))->result_array();				
		if(empty($label))
			return $field;
		else
		{			
			$return = $label[0]['strLabel'];
			
			if($label[0]['strRequired'])
				$return.=" *";
			
			return $return;
		}
    }

}

if (!function_exists('get_type_id')) {

    function get_type_id($type) {
        $ci = load_ci();		
		$type = $ci->db->get_where('tb_mas_content_types',array('strName'=>$type))->first_row();				
		if(empty($type))
			return 1;
		else
		{									
			return $type->intID;
		}
    }

}

if (!function_exists('get_type_name')) {

    function get_type_name($id) {
        $ci = load_ci();		
		$type = $ci->db->get_where('tb_mas_content_types',array('intID'=>$id))->first_row();				
		if(empty($type))
			return 1;
		else
		{									
			return $type->strName;
		}
    }

}

if (!function_exists('get_name_from_id')) {

    function get_name_from_id($id,$table) {
        $ci = load_ci();		
		$type = $ci->db->get_where($table,array('intID'=>$id))->first_row();				
		if(empty($type))
			return 1;
		else
		{
			if(isset($type->strName))
				return $type->strName;
			elseif(isset($type->strTitle))
				return $type->strTitle;
			elseif(isset($type->strRegionName1))
				return $type->strRegionName1;
		}
    }

}

if (!function_exists('get_enum_values')) {
	
	function get_enum_values( $table, $field )
	{
		
		$ci = load_ci();
		$type = $ci->db->query( "SHOW COLUMNS FROM `".$table."` WHERE Field = '{$field}'" )->row( 0 )->Type;		
		preg_match('/^enum\((.*)\)$/', $type, $matches);		
		
		foreach( explode(',', $matches[1]) as $value )
		{
			 $enum[] = trim( $value, "'" );
		}
		
		return $enum;
	}
}

if (!function_exists('pw_hash')) {

    function pw_hash($string) {
        $ci = load_ci();
		$ci->load->library("salting");

		$key = date('YmdHis');
		$key.= $ci->config->item("encryption_key");
				
		$ci->salting->set_hash(md5($key));
		//$ci->salting->set_hash(md5($ci->config->item("encryption_key")));
		$ci->salting->set_string($string);
		return $ci->salting->hash_string();
    }
}
if (!function_exists('pw_unhash')) {

    function pw_unhash($string) {
        $ci = load_ci();
		$ci->load->library("salting");

/* 		$key = date('YmdHis');
		$key.= $ci->config->item("encryption_key");
		
		$ci->salting->set_hash(md5($key)); */
		//$ci->salting->set_hash(md5($ci->config->item("encryption_key")));
		$ci->salting->set_string($string);
		return $ci->salting->unhash_string();;
    }
}